package com.live.dengdengtv.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.live.dengdengtv.R;
import com.live.dengdengtv.adapter.ConCardAdpter;
import com.live.dengdengtv.adapter.ConversationAdpter;
import com.live.dengdengtv.bean.gson.ConversationCard;
import com.live.dengdengtv.utils.EmptyViewUtil;
import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMManager;
import com.tencent.TIMMessage;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyf on 2016/9/22.
 */
public class ConversationFrag extends BaseFragment{

    private static final String TAG = "StrangerMsgFrag";
    private View contentView;
    private ListView listView;
    private List<ConversationCard> conversationList;
    private BaseAdapter adapter;
    private ConversationItemClick onItemClickListener;
    private View emptyView;

    public void setOnItemClickListener(ConversationItemClick onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setListData(List<ConversationCard> conversationList){
        this.conversationList = conversationList;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(contentView == null) {
//            listView = new ListView(getActivity());
//            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
//                    ViewGroup.LayoutParams.MATCH_PARENT,
//                    ViewGroup.LayoutParams.MATCH_PARENT);
//            listView.setLayoutParams(lp);
//            listView.setDivider(new ColorDrawable(0xFFE5E5E5));
//            listView.setDividerHeight(2);

            contentView = inflater.inflate(R.layout.msg_listview,container,false);
            listView = (ListView) contentView.findViewById(R.id.listView);

            emptyView = contentView.findViewById(R.id.emptyView);
//            listView.setEmptyView(emptyView);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    onItemClickListener.onConversationItemClick(conversationList.get(position).user_id);
                }
            });

            adapter = new ConCardAdpter(getActivity(), conversationList);
            listView.setAdapter(adapter);
        }
        return contentView;
    }

    public void notifyDataSetChanged(){
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
        if(emptyView !=null && conversationList.size()==0){
            listView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//        View emptyView = EmptyViewUtil.getEmptyView(getContext(),"这里什么都没有哦~");
//        ((ViewGroup)listView.getParent()).addView(emptyView);
//        listView.setEmptyView(emptyView);
//        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        onItemClickListener = null;
    }

}
